<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Blog
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: Core.php 8352 2011-01-31 22:30:22Z jung $
 * @author     Jung
 */

/**
 * @category   Application_Extensions
 * @package    Blog
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class Sblog_Api_Behavior extends Core_Api_Abstract
{
	public function create($values){
		// Process
	    $table = Engine_Api::_()->getItemTable('sblogs');
	    $db = $table->getAdapter();
	    $db->beginTransaction();
	    
	    $sblog = $table->createRow();
	    $sblog->setFromArray($values);	
	    $sblog->created = time();
	    $sblog->owner_id= $this->getOwnerId();
	    $sblog->save(); 	
	    
	    $this->auth($sblog);
	    
	    
	    try{

	      // Commit
	      $db->commit();   
	      return $sblog->sblogs_id; 
	    }
	    catch (Exception $e){
	      $db->rollBack();
	      throw $e;    	
	    } 
	}
    public function delete($sblogs_id){
    	$sblog = Engine_Api::_()->getItem('sblogs',$sblogs_id);	
        $db = $sblog->getTable()->getAdapter();
        $db->beginTransaction();
	    try
	    {
	      $sblog->delete();
	      $db->commit();
	    }
	    catch( Exception $e )
	    {
	      $db->rollBack();
	      throw $e;
	    }
	    return true ;
    }
    public function checkBeforeDelete($sblogs_id){
    	$sblog = Engine_Api::_()->getItem('sblogs',$sblogs_id);
    	if( !$sblog ){
    		return false;
    	}
    	return true;
    }
    public function edit($values,$sblogs_id){
    	$sblog = Engine_Api::_()->getItem('sblogs',$sblogs_id);
        $sblog->setFromArray($values);
	    $sblog->changed = time();
        // Process
	    //$db = Engine_Db_Table::getDefaultAdapter();
	    $db = $db = $sblog->getTable()->getAdapter();
	    $db->beginTransaction();
	    try{
	        $sblog->save();
	    	$db->commit();	
	    }catch (Exception $e){
	    	$db->rollBack();
	      	throw $e;   	
	    }
    }
    public function getBlogEntryValue($sblogs_id){
    	$sblog = Engine_Api::_()->getItem('sblogs',$sblogs_id);
    	return $sblog->toArray();
    }
    protected  function getOwnerId(){
    	 $viewer = Engine_Api::_()->user()->getViewer();
    	 return $viewer->getIdentity();   	 	
    }
    public function auth($sblog,$values = null){
    	$auth = Engine_Api::_()->authorization()->context;
      	$roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
        
      	if( empty($values['auth_view']) ) {
        	$values['auth_view'] = 'everyone';
      	}

      	if( empty($values['auth_comment']) ) {
        	$values['auth_comment'] = 'everyone';
      	}

      	$viewMax = array_search($values['auth_view'], $roles);
      	$commentMax = array_search($values['auth_comment'], $roles);
        
      	foreach( $roles as $i => $role ) {
      		
        	$auth->setAllowed($sblog, $role, 'view', ($i <= $viewMax));
        	$auth->setAllowed($sblog, $role, 'comment', ($i <= $commentMax));
      	}
    }
}